package org.example.ttms.entityClass.ticket;

import javafx.collections.FXCollections;
import org.example.ttms.controller.UserController;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.User;
import org.example.ttms.entityClass.train.Trips;

import java.util.HashMap;
import java.util.List;

public class RefundTicket extends Ticket{
    //退票
    public void refundTicket(Ticket ticket){
        if(refundTicketJudgement(ticket)){
            if(alert("退票")){
                delTicket(ticket);
            }
        }
    }

    //退票评判
    public boolean refundTicketJudgement(Ticket ticket){
        Trips trips = new Trips();
        trips = trips.obtainTrips(ticket.getId());
        Pop_ups popUps = new Pop_ups();
        if(ticket.getStatus().equals("已退票")){
            popUps.pop_ups3("该车票已退");
            return false;
        }
        if(trips.getStatus().equals("出发")){
            popUps.pop_ups3("该车次已出发,无法退票");
            return false;
        }
        if(trips.getStatus().equals("到达")){
            popUps.pop_ups3("该车次已到达,无法退票");
            return false;
        }
        return true;
    }
    //删除车票
    public void delTicket(Ticket ticket){
        //重置车票
        ticket.setStatus("已退票");
        HashMap<String, Ticket> hashMap = obtainAllTicket();
        hashMap.put(ticket.getTicketId(), ticket);
        storageTicket(FXCollections.observableArrayList(hashMap.values()));
        //车次票数增加
        Trips trips = new Trips();
        HashMap<String, Trips> tripsHashMap = trips.obtainAllTrips3();
        trips = tripsHashMap.get(ticket.getId());
        trips.setSellTicketNum(trips.getSellTicketNum()-1);
        tripsHashMap.put(ticket.getId(), trips);
        trips.storageTrips(FXCollections.observableArrayList(tripsHashMap.values()));
    }
}
